package com.study.servlet.session.prac01;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* @Name: LoginUISessionServlet
* @Description: Session应用：防止表单重复提交
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-3 下午3:27:00 
* @Version: V1.0
 */
public class LoginUISessionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		//创建令牌环：全球唯一字符串
		//使用UUID：MAC地址+随机数
		String token = UUID.randomUUID().toString() ;
		System.out.println(token);
		//放入到Session当中
		HttpSession session = request.getSession() ;
		session.setAttribute("TOKEN", token) ;
		out.write("<form action='"+request.getContextPath()+"/servlet/LoginSessionServlet' method='post'>");
		out.write("用户名：<input type='text' name='username'/><br/>");
		out.write("<input type='hidden' name='token' value='"+token+"'/>");
		out.write("<input type='submit' value='登录'/>");
		out.write("</form>");
		out.close() ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
